package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.StrategyCollectMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCollectServiceImpl implements IStrategyCollectService {
    @Autowired
    private StrategyCollectMapper strategyCollectMapper;

    @Override
    public StrategyCollect getCollectByUserId(Long userId, Long strategyId, Long state) {
        StrategyCollect strategyCollect = strategyCollectMapper.getCollectByUserId(userId,strategyId,state);
        return strategyCollect;
    }

    @Override
    public boolean selectCollect(Long userId, Long strategyId) {
        StrategyCollect strategyCollect = strategyCollectMapper.selectExist(userId,strategyId);
        boolean flag = true;
        Date time = new Date();
        if(strategyCollect != null){
            boolean state = strategyCollect.isState();
            state = !state;
            strategyCollectMapper.changeState(userId,strategyId,time,state);
            flag = state;
        }else{
            StrategyCollect entity = new StrategyCollect();
            entity.setTime(time);
            User user = new User();
            user.setId(userId);
            entity.setUser(user);
            Strategy strategy = new Strategy();
            strategy.setId(strategyId);
            entity.setUser(user);
            entity.setStrategy(strategy);
            entity.setState(true);
            strategyCollectMapper.insertCollect(entity);
            flag = true;
        }
        return flag;

    }

    @Override
    public Integer getCollectNumsByStrategyId(Long strategyId) {
        return strategyCollectMapper.getCollectNumsByStrategyId(strategyId);
    }

    @Override
    public PageInfo<StrategyCollect> query(QueryObject qo, Long userId) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()," sc.time desc");
        List<StrategyCollect> list = strategyCollectMapper.query(qo,userId);
        return new PageInfo<>(list);
    }
}
